package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class StudyjdhActivity extends Activity implements OnClickListener {
    //学习机顶盒遥控器
    private HongWaiSB h;
    private MyApplication m;
    private boolean flag1;
    private EditText et;
    public int id;
    public ArrayList<String> button;
    public ArrayList<String> code;
    public String name;
    private ImageView ParentImageView;
    LinearLayout btl1;
    LinearLayout btl2;
    LinearLayout btl3;


    Handler hand = new Handler() {

        public void handleMessage(android.os.Message msg) {

            if (msg.what == 2) {
                if(dialog.isShowing()){
                    dialog.dismiss();
                }
                HttpConnection.showToast(getBaseContext(),"学习成功");

                int d = ParentImageView.getId();

                if (d == R.id.a00) {
                    bt0.setImageResource(R.drawable.studied_0);

                } else if (d == R.id.a01) {
                    bt1.setImageResource(R.drawable.studied_1);

                } else if (d == R.id.a02) {

                    bt2.setImageResource(R.drawable.studied_2);
                } else if (d == R.id.a03) {
                    bt3.setImageResource(R.drawable.studied_3);


                } else if (d == R.id.a04) {
                    bt4.setImageResource(R.drawable.studied_4);

                } else if (d == R.id.a05) {
                    bt5.setImageResource(R.drawable.studied_5);

                } else if (d == R.id.a06) {
                    bt6.setImageResource(R.drawable.studied_6);

                } else if (d == R.id.a07) {
                    bt7.setImageResource(R.drawable.studied_7);

                } else if (d == R.id.a08) {
                    bt8.setImageResource(R.drawable.studied_8);

                } else if (d == R.id.a09) {

                    bt9.setImageResource(R.drawable.studied_9);
                } else if (d == R.id.a0pindaojia) {
                    btl2.setBackgroundResource(R.drawable.anniujiajian);

                } else if (d == R.id.a0pindaojian) {
                    btl2.setBackgroundResource(R.drawable.anniujiajian);

                } else if (d == R.id.a0yinliangjia) {
                    btl1.setBackgroundResource(R.drawable.anniujiajian);
                } else if (d == R.id.a0yiliangjian) {
                    btl1.setBackgroundResource(R.drawable.anniujiajian);
                } else if (d == R.id.a0shang) {
                    btl3.setBackgroundResource(R.drawable.anniuwaiquan);

                } else if (d == R.id.a0xia) {
                    btl3.setBackgroundResource(R.drawable.anniuwaiquan);

                } else if (d == R.id.a0zuo) {
                    btl3.setBackgroundResource(R.drawable.anniuwaiquan);

                } else if (d == R.id.a0you) {
                    btl3.setBackgroundResource(R.drawable.anniuwaiquan);

                } else if (d == R.id.a0daoshi) {
                    badaoshi.setImageResource(R.drawable.buttondaoshistudying);

                } else if (d == R.id.a0caidan) {

                    bacaidan.setImageResource(R.drawable.studied_caidan);

                } else if (d == R.id.a0fanhui) {
                    btafanhui.setImageResource(R.drawable.studied_fanhui);

                } else if (d == R.id.a0dianyuan) {
                    btadianyuan.setImageResource(R.drawable.studied_kaiguan);
                }

            }
        };
    };
    private String s;
    private AlertDialog.Builder builder;
    private ImageView bt0;
    private ImageView bt1;
    private ImageView bt2;
    private ImageView bt3;
    private ImageView bt4;
    private ImageView bt5;
    private ImageView bt6;
    private ImageView bt7;
    private ImageView bt8;
    private ImageView bt9;
    private ImageView btashang;
    private ImageView btaxia;
    private ImageView btaqueren;
    private ImageView btafanhui;
    private ImageView btazuo;
    private ImageView btayou;
    private ImageView btapindaojia;
    private ImageView btapindaojian;
    private ImageView btayinliangjia;
    private ImageView btayinlianjian;
    private ImageView btadianyuan;
    private ImageView btabaocun;
    private ImageView bacaidan;
    private ImageView badaoshi;
    private Dialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studyji_ding_he);
        info();
    }

    private void info() {
        m = (MyApplication) getApplication();
        Intent it = getIntent();
        s = it.getStringExtra("num");
        name = it.getStringExtra("name");
        String flag = it.getStringExtra("flag");
        button = it.getStringArrayListExtra("button");

        code = it.getStringArrayListExtra("code");
        id = it.getIntExtra("id", -1);

        if (id != -1) {
            for (int i = 0; i < button.size(); i++) {

                m.YuanSM.put(Integer.parseInt(button.get(i)), code.get(i));

            }
        }
        btl1 = (LinearLayout) findViewById(R.id.anniujiajian);
        btl2 = (LinearLayout) findViewById(R.id.anniujiajian2);
        btl3 = (LinearLayout) findViewById(R.id.waiquan);

        if (flag != null && "a".equals(flag)) {
            flag1 = true;


        }

        m.hand = hand;
        h = new HongWaiSB();
        h.cmd = "1012";
        h.extra = new Extra();

        h.status = "02";
        if (!flag1) {
            h.status = "01";
            h.extra.codeIndex = s;
        }

        h.extra.type = m.type;


            h.ieee = m.ieee_ykq;
            h.port = m.port_ykq;
            h.mac=m.mac_ykq;

        bt0 = (ImageView) findViewById(R.id.a00);
        bt0.setOnClickListener(this);
        bt1 = (ImageView) findViewById(R.id.a01);
        bt1.setOnClickListener(this);
        bt2 = (ImageView) findViewById(R.id.a02);
        bt2.setOnClickListener(this);
        bt3 = (ImageView) findViewById(R.id.a03);
        bt3.setOnClickListener(this);
        bt4 = (ImageView) findViewById(R.id.a04);
        bt4.setOnClickListener(this);
        bt5 = (ImageView) findViewById(R.id.a05);
        bt5.setOnClickListener(this);
        bt6 = (ImageView) findViewById(R.id.a06);
        bt6.setOnClickListener(this);
        bt7 = (ImageView) findViewById(R.id.a07);
        bt7.setOnClickListener(this);
        bt8 = (ImageView) findViewById(R.id.a08);
        bt8.setOnClickListener(this);
        bt9 = (ImageView) findViewById(R.id.a09);
        bt9.setOnClickListener(this);
        btashang = (ImageView) findViewById(R.id.a0shang);
        btashang.setOnClickListener(this);
        btaxia = (ImageView) findViewById(R.id.a0xia);
        btaxia.setOnClickListener(this);
        btaqueren = (ImageView) findViewById(R.id.a0queren);
        btaqueren.setOnClickListener(this);
        btafanhui = (ImageView) findViewById(R.id.a0fanhui);
        btafanhui.setOnClickListener(this);
        btazuo = (ImageView) findViewById(R.id.a0zuo);
        btazuo.setOnClickListener(this);
        btayou = (ImageView) findViewById(R.id.a0you);
        btayou.setOnClickListener(this);
        btapindaojia = (ImageView) findViewById(R.id.a0pindaojia);
        btapindaojia.setOnClickListener(this);
        btapindaojian = (ImageView) findViewById(R.id.a0pindaojian);
        btapindaojian.setOnClickListener(this);
        btayinliangjia = (ImageView) findViewById(R.id.a0yinliangjia);
        btayinliangjia.setOnClickListener(this);
        btayinlianjian = (ImageView) findViewById(R.id.a0yiliangjian);
        btayinlianjian.setOnClickListener(this);
        btadianyuan = (ImageView) findViewById(R.id.a0dianyuan);
        btadianyuan.setOnClickListener(this);
        btabaocun = (ImageView) findViewById(R.id.a0baocun);
        btabaocun.setOnClickListener(this);
        bacaidan = (ImageView) findViewById(R.id.a0caidan);
        bacaidan.setOnClickListener(this);

        LinearLayout ivfanhui = (LinearLayout) findViewById(R.id.iv_yaokongqi);

        ivfanhui.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        badaoshi = (ImageView) findViewById(R.id.a0daoshi);
        bacaidan.setOnClickListener(this);

        dialog=new Dialog(StudyjdhActivity.this,R.style.dialog);
        dialog.setContentView(R.layout.dialog_study);
        dialog.setCanceledOnTouchOutside(false);

    }


    // {"cmd":"1012","ieee":"


    // 00124B000AF83E16","port":"08","status":"02",extra{"type":"04"}}
    public void send1(int i) {

        if (flag1) {
            String l = "";
            l = JSON.toJSONString(h);
            if (!Gateway.MacSocke.containsKey(m.mac_ykq)) {
                if(m.flag3){
                m.c.send(l);}}else{
                String cmd = GatewayRequestUtil.createInfraredForwardingStr2(m.ieee_ykq, m.port_ykq,m.type,"00");
                SendUtil.Mac = m.mac_ykq;
                SendUtil.cmd = cmd;
                SendUtil.hand.sendEmptyMessage(0);
            }
            dialog.show();
            m.AnNiuId = i;
        } else {
            String l = "";
            h.extra.codeValue = "" + i;
            l = JSON.toJSONString(h);
            if (!Gateway.MacSocke.containsKey(m.mac_ykq)) {
                if(m.flag3){
                m.c.send(l);}
            }else{
                String cmd = GatewayRequestUtil.createInfraredForwardingStr(m.ieee_ykq, m.port_ykq,m.type,Integer.parseInt(h.extra.codeIndex),h.extra.codeValue);
                SendUtil.Mac = m.mac_ykq;
                SendUtil.cmd = cmd;
                SendUtil.hand.sendEmptyMessage(0);
            }
        }

    }


























    @Override
    public void onClick(View v) {


        switch (v.getId()) {

            case R.id.a00:
                ParentImageView = btaxia;
                bt0.setImageResource(R.drawable.studying_0);
                send1(23);
                break;


            case R.id.a01:
                ParentImageView = btaxia;
                bt1.setImageResource(R.drawable.studying_1);
                send1(3);
                break;

            case R.id.a02:
                ParentImageView = btaxia;
                bt2.setImageResource(R.drawable.studying_2);
                send1(5);
                break;

            case R.id.a03:
                ParentImageView = btaxia;
                bt3.setImageResource(R.drawable.studying_3);
                send1(7);
                break;

            case R.id.a04:
                ParentImageView = btaxia;
                bt4.setImageResource(R.drawable.studying_4);
                send1(9);
                break;


            case R.id.a05:
                ParentImageView = btaxia;
                bt5.setImageResource(R.drawable.studying_5);
                send1(11);
                break;

            case R.id.a06:
                ParentImageView = btaxia;
                bt6.setImageResource(R.drawable.studying_6);
                send1(13);

                break;

            case R.id.a07:
                ParentImageView = btaxia;
                bt7.setImageResource(R.drawable.studying_7);
                send1(15);
                break;

            case R.id.a08:
                ParentImageView = btaxia;
                bt8.setImageResource(R.drawable.studying_8);
                send1(17);
                break;

            case R.id.a09:
                ParentImageView = btaxia;
                bt9.setImageResource(R.drawable.studying_9);
                send1(19);
                break;

            case R.id.a0caidan:
                ParentImageView = btaxia;
                bacaidan.setImageResource(R.drawable.studying_caidan);
                send1(45);
                break;

            case R.id.a0dianyuan:
                ParentImageView = btaxia;
                btadianyuan.setImageResource(R.drawable.studying_kaiguan);
                send1(1);
                break;

            case R.id.a0fanhui:
                ParentImageView = btaxia;
                btafanhui.setImageResource(R.drawable.studying_fanhui);
                send1(25);
                break;


            case R.id.a0pindaojia:
                btapindaojia.setImageResource(R.drawable.press_jia);
                btl2.setBackgroundResource(R.drawable.anniujia);
                ParentImageView = btapindaojia;
                send1(41);
                break;
            case R.id.a0pindaojian:
                btapindaojian.setImageResource(R.drawable.pressjiajianjian);
                btl2.setBackgroundResource(R.drawable.studyingjian);
                ParentImageView = btapindaojian;
                send1(43);
                break;
            case R.id.a0yinliangjia:
                btayinliangjia.setImageResource(R.drawable.press_jia);
                btl1.setBackgroundResource(R.drawable.anniujia);
                ParentImageView = btayinliangjia;
                send1(37);
                break;
            case R.id.a0yiliangjian:
                btayinlianjian.setImageResource(R.drawable.pressjiajianjian);
                btl1.setBackgroundResource(R.drawable.studyingjian);
                ParentImageView = btayinlianjian;
                send1(39);
                break;
            case R.id.a0shang:
                btashang.setImageResource(R.drawable.studiedshang);
                btl3.setBackgroundResource(R.drawable.studied_quanshang);
                ParentImageView = btashang;
                send1(27);
                break;
            case R.id.a0xia:
                btaxia.setImageResource(R.drawable.studied_xia);
                btl3.setBackgroundResource(R.drawable.studying_quanxia);
                ParentImageView = btaxia;
                send1(35);
                break;
            case R.id.a0zuo:
                btazuo.setImageResource(R.drawable.studied_zuo);
                btl3.setBackgroundResource(R.drawable.studying_quanzuo);
                ParentImageView = btazuo;
                send1(29);
                break;
            case R.id.a0you:
                btayou.setImageResource(R.drawable.studied_you);
                btl3.setBackgroundResource(R.drawable.studied_quanyou);
                ParentImageView = btayou;
                send1(33);
                break;
            case R.id.a0queren:
                btaqueren.setImageResource(R.drawable.studying_ok);
                ParentImageView = btayou;
                send1(31);
                break;

            case R.id.a0daoshi:
                badaoshi.setImageResource(R.drawable.buttondaoshistudying);
                ParentImageView = btayou;
                send1(21);
                break;


            case R.id.a0baocun:


                if (id != -1) {

                    HongWaiSB hs = new HongWaiSB();
                    hs.mac = m.mac_ykq;
                    hs.ieee=m.ieee_ykq;
                    hs.port=m.port_ykq;
                    hs.kind = m.type;
                    hs.name = name;
                    hs.controllerList = new ArrayList<controllerList>();

                    if (flag1) {

                        hs.selfLearning = "1";
                        hs.id = id + "";
                        System.out.print(m.YuanSM.size());

                        for (Integer key : m.YuanSM.keySet()) {
                            controllerList c = new controllerList();
                            c.button = key + "";
                            c.originalCode = m.YuanSM.get(key);
                            hs.controllerList.add(c);

                        }






				/*
                    for (int i = 0; i < m.YuanSM.size(); i++) {
						controllerList c = new controllerList();
						c.button = m.AnNiuId.get(i)+"";
						c.originalCode = m.YuanSM.get(i);
						hs.controllerList.add(c);
					}

					for(int i=0;i<button.size();i++) {
						int e = 0;
						for (int j = 0; j < m.AnNiuId.size(); j++) {
							if (button.get(i).equals(m.AnNiuId.get(j))) {
								e = 1;
							}
						}
						if (e == 0) {
							controllerList c = new controllerList();
							String s = button.get(i);
							c.button = s;
							c.originalCode = code.get(i);
							hs.controllerList.add(c);

						}*/



				/*for(int i=0;i<ImageView.size();i++){
                    controllerList c = new controllerList();
					String s = ImageView.get(i);

					 c.ImageView=s;

					c.originalCode=code.get(i);
					hs.controllerList.add(c);
				}
				for (int i = 0; i < m.YuanSM.size(); i++) {
					controllerList c = new controllerList();

					c.ImageView = m.AnNiuId.get(i)+"";

					c.originalCode = m.YuanSM.get(i);
					hs.controllerList.add(c);

				}*/

                    } else {
                        hs.id = id + "";
                        hs.selfLearning = "0";
                        for (int i = 0; i < 25; i++) {
                            controllerList c = new controllerList();
                            c.button = 2 * i + 1 + "";
                            c.num = s;
                            hs.controllerList.add(c);

                        }

                    }

                    String k = JSON.toJSONString(hs);
                    k = AESUtil.encrypt(k);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(k, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/addController",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");
                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if (arg0.result != null && !("".equals(arg0.result))) {
                                        arg0.result = AESUtil.decrypt(arg0.result);
                                        JSONObject j = JSONObject.parseObject(arg0.result);
                                        String s = j.getString("msg");

                                        if ("成功".equals(s)) {
                                            m.AnNiuId = -1;
                                            m.YuanSM.clear();
                                            Intent it = new Intent(StudyjdhActivity.this, MyYaoKongActivity.class);
                                            startActivity(it);
                                            finish();
                                        }else{

                                            HttpConnection.showToast(StudyjdhActivity.this,"服务器错误");
                                    }

                                    }else{
                                        HttpConnection.showToast(StudyjdhActivity.this,"服务器错误");
                                    }
                                }

                            });


                } else {


                    Builder builder = new AlertDialog.Builder(StudyjdhActivity.this);
                    builder.setTitle("请输入");
                    builder.setIcon(android.R.drawable.ic_dialog_info);
                    et = new EditText(StudyjdhActivity.this);
                    builder.setView(et);
                    builder.setNegativeButton("取消", null);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            HongWaiSB hs = new HongWaiSB();

                            hs.controllerList = new ArrayList<controllerList>();
                            String s1 = et.getText().toString();
                            String str2 = s1.replaceAll(" ", "");
                            if (!"".equals(str2)) {
                                hs.name = et.getText().toString();
                                hs.mac = m.mac_ykq;
                                hs.ieee=m.ieee_ykq;
                                hs.port=m.port_ykq;
                                hs.kind = m.type;
                                if (flag1) {
                                    hs.selfLearning = "1";
                                    for (Integer key : m.YuanSM.keySet()) {
                                        controllerList c = new controllerList();
                                        c.button = key + "";
                                        c.originalCode = m.YuanSM.get(key);
                                        hs.controllerList.add(c);

                                    }

                                } else {
                                    hs.selfLearning = "0";
                                    for (int i = 0; i < 25; i++) {
                                        controllerList c = new controllerList();
                                        c.button = 2 * i + 1 + "";
                                        c.num = s;
                                        hs.controllerList.add(c);

                                    }
                                }

                                String k = JSON.toJSONString(hs);
                                k = AESUtil.encrypt(k);
                                HttpUtils httpUtils = new HttpUtils();
                                RequestParams params = new RequestParams();
                                params.setHeader("Content-Type", "application/json");
                                try {
                                    params.setBodyEntity(new StringEntity(k, "utf-8"));
                                } catch (UnsupportedEncodingException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/addController",
                                        params, new RequestCallBack<String>() {

                                            @Override
                                            public void onFailure(HttpException arg0, String arg1) {
                                                HttpConnection.showToast(getBaseContext(),"网络异常");
                                            }

                                            @Override
                                            public void onSuccess(ResponseInfo<String> arg0) {
                                                if (arg0.result != null && !("".equals(arg0.result))) {
                                                    arg0.result = AESUtil.decrypt(arg0.result);
                                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                                    String s = j.getString("msg");

                                                    if ("成功".equals(s)) {
                                                        m.AnNiuId = -1;
                                                        m.YuanSM.clear();
                                                        Intent it = new Intent(StudyjdhActivity.this, MyYaoKongActivity.class);
                                                        startActivity(it);
                                                        finish();

                                                    }else{
                                                        HttpConnection.showToast(StudyjdhActivity.this,"服务器错误");
                                                    }

                                                }else{
                                                    HttpConnection.showToast(StudyjdhActivity.this,"服务器错误");
                                                }
                                            }

                                        });

                            } else {
                                HttpConnection.showToast(getBaseContext(),"请输入名称");

                            }

                        }
                    });

                    Dialog dialog = builder.create();
                    dialog.show();
                }

                break;

        }

    }

}

	

